package {
	import flash.display.Sprite;
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileReference;
	import com.bit101.components.*;
	import cn.rayYee.utils.DisplayManager;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	public class MapEdit extends Sprite
	{
		private var backgroundMC :Sprite;
		private var masklayer:Sprite;//遮罩层，区分怪物行走区域 & 可建造区域。
		private var full_width :int  = 900;
		private var full_height :int = 560;
		public var grid_size :int=20;
		public function MapEdit() {
			
			backgroundMC  = new Sprite();
			this.addChild(this.backgroundMC);
			drawBackground();
			initToolBar();
		}
		
		private function initToolBar() {
			var tipPanel:Panel = new Panel(this, 700, 0);
			tipPanel.setSize(200, 600);
			
			
			
			var acc:Accordion = new Accordion(tipPanel);
			acc.setSize(200, 300);
			var section1:Window = acc.getWindowAt(0) as Window;
			var section2:Window = acc.getWindowAt(1) as Window;
			section1.title = "Road";
			section2.title = "Tree & building";
			
			var hslabel:Label = new Label(tipPanel,100,300);
			var hSlider:HUISlider = new HUISlider(tipPanel, 0, 300, "Grid Size", function (e:Event):void {
				hslabel.text = String(hSlider.value);
				if (hSlider.value > 20 && hSlider.value < 50) {
					grid_size = hSlider.value;
				}
				drawBackground();
			});
			hSlider.minimum = 20;
			hSlider.maximum = 20;
			
			var levelBox:ComboBox = new ComboBox(tipPanel, 0, 320, "Choose level");
			levelBox.addItem( { label:"level1" } );
			levelBox.addItem( { label:"level2" } );
			levelBox.addItem( { label:"level3" } );
			
			var _fileRef:FileReference = new FileReference();//用于保存文件
			
			var savebtn:PushButton = new PushButton(tipPanel, 0, 500, "Save to local", function(e:MouseEvent):void {
				var ba:XML = new XML();
				var savename:String = levelBox.selectedItem.label + ".xml";
				_fileRef.save(ba, savename);//保存到磁盘，会出现个系统保存对话框。
			});
			
			var uploadbtn:PushButton = new PushButton(tipPanel, 100, 500, "Upload to server", function(e:MouseEvent):void {
				var request:URLRequest = new URLRequest("");
				request.method=URLRequestMethod.POST;
				_fileRef.upload(request);
			});
			
			var lg:logo = new logo();
			addChild(lg);
		}
		
		/**
		 * Draw the background graphics for this game
		 * 
		 * @return void
		 */
		private function drawBackground() :void
		{
			var g :Graphics = this.backgroundMC.graphics;
			g.clear (); 
			g.beginFill(0xe4e4e4);
			g.drawRect(0, 0, this.full_width, this.full_height);
			g.endFill();
			
			// Draw vertical hash marks
			g.lineStyle(1, 0xaaaaaa);
			for (var i :int = 0; i < Math.round(this.full_width / this.grid_size); i++)
			{
				g.moveTo(i * this.grid_size, 0);
				g.lineTo(i * this.grid_size, this.full_height);
			}
			
			// Draw horizontal hash marks
			for (i = 0; i < Math.round(this.full_height / this.grid_size); i++)
			{
				g.moveTo(0, i * this.grid_size);
				g.lineTo(this.full_width, i * this.grid_size);
			}
			
			
		}
		
		
	}
}